﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Util
{
    /// <summary>
    /// 数据转换类型
    /// </summary>
    public static class Conv
    {
        public static string ToJson(object data)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(data);
        }

        public static T ParseJson<T>(string json)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
        }

        /// <summary>
        /// 转换成Int32类型
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="defaultVal">转换失败默认值</param>
        /// <returns></returns>
        public static int ToInt(string str, int defaultVal = 0)
        {
            int val;
            if (Int32.TryParse(str, out val))
            {
                return val;
            }
            return defaultVal;
        }

        /// <summary>
        /// 转换成short类型
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="defaultVal">转换失败默认值</param>
        /// <returns></returns>
        public static short ToShort(string str, short defaultVal = 0)
        {
            short val;
            if (short.TryParse(str, out val))
            {
                return val;
            }
            return defaultVal;
        }

        /// <summary>
        /// 转换成DateTime类型
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>转换失败返回1970-1-1</returns>
        public static DateTime ToDate(string str)
        {
            DateTime date;
            if (DateTime.TryParse(str, out date) == false)
            {
                date = new DateTime(1970, 1, 1);
            }
            return date;
        }

        /// <summary>
        /// 转换成DateTime类型
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="defaultDate">转换失败返回值</param>
        public static DateTime ToDate(string str, DateTime defaultDate)
        {
            DateTime date;
            if (DateTime.TryParse(str, out date) == false)
            {
                date = defaultDate;
            }
            return date;
        }

        /// <summary>
        /// 返回中文星期文本。（一二三四五六日）
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns></returns>
        public static string ToChWeekName(DateTime dt)
        {
            switch (dt.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return "日";
                case DayOfWeek.Saturday:
                    return "六";
                case DayOfWeek.Friday:
                    return "五";
                case DayOfWeek.Thursday:
                    return "四";
                case DayOfWeek.Wednesday:
                    return "三";
                case DayOfWeek.Tuesday:
                    return "二";
                case DayOfWeek.Monday:
                    return "一";

            }
            return string.Empty;
        }
    }
}
